package com.springboot.blog.controller;

import com.springboot.blog.entity.Article;
import com.springboot.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService service;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/{id:\\d+}.html")
    public String selectOne(@PathVariable Integer id, Model m) {
        Article article = service.selectByPrimaryKey(id);
        m.addAttribute(article);
        return "article";
    }
    @GetMapping(value = "/{id:\\d+}")
    public String selectOneRedirect(@PathVariable Integer id) {
        return "redirect:/article/"+id+".html";
    }
}
